package org.bulu.project.web.controller.video;

import org.bulu.project.common.result.Result;
import org.bulu.project.system.model.dto.VideoUploadInfoDTO;
import org.bulu.project.system.model.entity.Video;
import org.bulu.project.system.service.VideoUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/upload")
public class VidoeController {
    @Autowired
    private VideoUploadService uploadService;
    /*
        videoUrl 是文件的本机地址
        return 返回上传后的地址
     */
    @PostMapping("/upload")
    public Result uploadVideo(@RequestParam("file") MultipartFile file) {
        try{
            // 返回上传结果
            return uploadService.UploadVideo(file);
        }catch(Exception e) {
            return Result.failed("服务器错误");
        }
    }

    /**
     * 添加视频投稿
     * @param coverUrl 封面文件
     * @param videoUrl  videoUrl视频的url地址
     * @param title 投稿标题
     * @param type  视频类型 1自制 2转载
     * @param auth  作者声明 0不声明 1未经允许禁止转载
     * @param mcid  主分区ID
     * @param scid  子分区ID
     * @param tags  标签
     * @param descr 简介
     * @return  响应对象
     * @RequestParam("coverUrl") String coverUrl,
     *                            @RequestParam("videoUrl") String videoUrl,
     *                            @RequestParam("title") String title,
     *                            @RequestParam("type") Integer type,
     *                            @RequestParam("auth") Integer auth,
     *                            @RequestParam("mcid") String mcid,
     *                            @RequestParam("scid") String scid,
     *                            @RequestParam("tags") String tags,
     *                            @RequestParam("descr") String descr
     */
    @PostMapping("/video/add")
    public Result addVideo(@RequestBody Video video) {
//        Video video = new Video();
//        video.setCoverUrl(coverUrl);
//        video.setTitle(title);
//        video.setVideoUrl(videoUrl);
//        video.setType(type);
//        video.setAuth(auth);
//        video.setMcId(mcid);
//        video.setScId(scid);
//        video.setTags(tags);
//        video.setDescr(descr);
        try {
            return Result.success(uploadService.addVideo(video));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed( "false");
        }
    }

    /**
     * 取消视频投稿，删除已经上传的文件
     * @param videoUrl 视频url
     * @return  响应对象
     */
    @PostMapping("/video/delete")
    public Result deleteVideo(@RequestParam("videoUrl") String videoUrl) {
        try {
            return uploadService.DeleteVideo(videoUrl);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("删除失败");
        }
    }

    /**
     * 删除视频
     * @param vid 视频id
     * @return  响应对象
     */
    @PostMapping("/video/deleteAll")
    public Result deleteVideo(@RequestParam("vid")Integer vid) {
        try {
            return uploadService.DeleteAllVideoInformation(vid);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("删除失败");
        }
    }

    /***
     * 上传封面
     * @param file
     * @return
     */
    @PostMapping("/cover")
    public Result uploadCover(@RequestParam("file") MultipartFile file){
        try{
            return uploadService.uploadCover(file);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("上传失败");
        }
    }

}
